/*
 * bind.c
 *
 *  Created on: 2009-5-28
 *      Author: root
 */

#include"mysocket.h"
#include"msgtype.h"
#include <sys/types.h>
#include <unistd.h>
#include<string.h>


int bind(int sockfd,const struct sockaddr *myaddr,int addrlen){
	msg message;
	sockmsg retmsg;
	int i,rv;
	memset(&message,0,sizeof(msg));
	message.mtype=MSG_USR_BIND;
	message.pid=getpid();
	message.sock=sockfd;
	message.msgqid=processqid;
	message.buflen=addrlen;
	//message.sock_addr=*myaddr;
	memcpy(message.buf,myaddr,addrlen);
	//for(i=0;i<message.buflen;i++){
		//message.buf[i]=*((char *)myaddr+i);
	//}
	if((rv=msgsnd(tcpipstackqid,&message,sizeof(msg)-sizeof(long),IPC_NOWAIT))<0){
		fprintf(stderr,"tcp/ip stack does not work!\n");
		return -1;
	}
	if((rv=msgrcv(processqid,&retmsg,sizeof(sockmsg)-sizeof(long),sockfd,0))<0){
		fprintf(stderr,"tcp/ip stack does not work!\n");
		return -1;
	}
	return retmsg.ret;
}
